package main;

import javax.microedition.lcdui.Graphics;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
public class Dialog {

    private static final int DIALOG_SHOW_HEIGHT = 40;
    private static final int DIALOG_INPUT_HEIGHT = 60;
    public boolean isShow;
    private String dialogString;
    private CustomTextBox textBox;
    public int typeInput;
    private boolean keyPad;

    public Dialog(int typeInput, boolean keyPad) {
        this.typeInput = typeInput;
        this.keyPad = keyPad;
    }

    public void setTypeInput(int type) {
        this.typeInput = type;
        if (textBox != null) {
            textBox.setTypeInput(typeInput);
        }
    }

    public void setString(String str) {
        this.dialogString = str;
    }

    public void setTextInBox(String str) {
        if (textBox != null) {
            textBox.setText(str);
        }
    }

    public void doKeyPound() {
        if (textBox != null) {
            textBox.doKeyPound();
        }
    }

    public void show(boolean isShow) {
        this.isShow = isShow;
        if (textBox != null) {
            textBox.setText("");
        }
    }

    public void paintShow(Graphics g, int left, int bottom, int width) {
        if (isShow) {
            g.setColor(0x000000);
            g.fillRoundRect(left, bottom - DIALOG_SHOW_HEIGHT, width, DIALOG_SHOW_HEIGHT, 10, 10);
            int stringDialogWidth = PiPoDesigner.stringWidth(dialogString);
            PiPoDesigner.drawString(g, dialogString, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE,
                    ((width - stringDialogWidth) >> 1) + left, bottom - DIALOG_SHOW_HEIGHT);
            g.setColor(0xffffff);
            g.fillRect(left + 5, bottom - DIALOG_SHOW_HEIGHT / 2, width - 10, (DIALOG_SHOW_HEIGHT >> 1) - 5);
            PiPoDesigner.drawCenterString(g, "OK", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, width / 2 + left, bottom - 20);
            g.drawRoundRect(left, bottom - DIALOG_SHOW_HEIGHT, width, DIALOG_SHOW_HEIGHT, 10, 10);
        }
    }

    public void paintInput(Graphics g, int left, int bottom, int width) {
        if (isShow) {
            if (textBox == null) {
                textBox = new CustomTextBox(PiPoDesigner.COLOR_WHITE, PiPoDesigner.COLOR_BLACK, false, keyPad);
                textBox.setTypeInput(typeInput);
            }
            g.setColor(0x000000);
            g.fillRoundRect(left, bottom - DIALOG_INPUT_HEIGHT, width, DIALOG_INPUT_HEIGHT, 10, 10);
            int stringDialogWidth = PiPoDesigner.stringWidth(dialogString);
            PiPoDesigner.drawString(g, dialogString, PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_WHITE,
                    ((width - stringDialogWidth) >> 1) + left, bottom - DIALOG_INPUT_HEIGHT);
            textBox.paint(g, left + 10, bottom - DIALOG_INPUT_HEIGHT + 17, width - 10, 20);
            g.setColor(0xffffff);
            PiPoDesigner.drawCenterString(g, "OK", PiPoDesigner.COLOR_BLACK, PiPoDesigner.COLOR_YELOW, width / 2 + left, bottom - 20);
            g.drawRoundRect(left, bottom - DIALOG_INPUT_HEIGHT, width, DIALOG_INPUT_HEIGHT, 10, 10);
        }
    }

    public void run() {
        if (isShow && textBox != null) {
            textBox.run();
        }
    }

    public void input(int key) {
        if (isShow && textBox != null) {
            textBox.inputChar(key);
        }
    }

    public void doSoftKeyRight() {
        textBox.doSoftKeyRight();
    }

    public String getText() {
        return textBox.getText();
    }
}
